package test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {
	private static String delimiter="\n";

	public static void main(String[] args) {

		String request=
			"GET http://www.google.com/ HTTP/1.1"+delimiter
			+"Host: www.google.com"+delimiter
			+"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8 (.NET CLR 3.5.30729)"+delimiter
			+"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"+delimiter
			+"Accept-Language: en-us,en;q=0.5"+delimiter
			+"Accept-Encoding: gzip,deflate"+delimiter
			+"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"+delimiter
			+"Keep-Alive: 300"+delimiter
			+"Proxy-Connection: keep-alive"+delimiter
			+delimiter;
		
		String response="";
		
		try {
			Socket socket=new Socket("localhost",8080);
			InputStream in = socket.getInputStream();
			OutputStream out = socket.getOutputStream();
			OutputStreamWriter osw = new OutputStreamWriter(out, "ASCII");
			osw.write(request);
			osw.flush();
			System.out.println("Sent");
			
			int c;
			StringBuilder sb = new StringBuilder();
			while ((c = in.read()) != -1) {
				sb.append((char) c);
				String end = sb.toString();
				if (end.endsWith("\n\n") || end.endsWith("\r\n\r\n") || end.endsWith("\r\r") ){
					response = sb.toString();
					System.out.println("---------------SERVER---------------");
					System.out.println(response);
					System.out.println("---------------SERVER---------------");
					break;
				}
			}
			System.out.println("died");
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
